package com.gill.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gill.exception.CommonServiceException;
import com.gill.service.HallService;
import com.gill.vo.BaseResponseVO;
import com.gill.vo.HallSavedReqVO;
import com.gill.vo.HallsReqVO;
import com.gill.vo.HallsRespVO;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/hall")
public class HallController {

    @Autowired
    private HallService hallService;

    /**
     * @Description: 获取播放厅列表
     */
    @GetMapping("/halls")
    public BaseResponseVO describeHalls(HallsReqVO hallsReqVO) throws CommonServiceException {

        hallsReqVO.checkParam();

        IPage<HallsRespVO> page = hallService.describeHalls(hallsReqVO);

        Map<String, Object> halls = descrbePageResult(page, "halls");

        return BaseResponseVO.success(halls);
    }

    /**
     * @Description: 新增播放厅
     */
    @PostMapping(value = "/add")
    public BaseResponseVO saveHall(@RequestBody HallSavedReqVO hallSavedReqVO) throws CommonServiceException {

        hallSavedReqVO.checkParam();

        hallService.saveHall(hallSavedReqVO);

        return BaseResponseVO.success();
    }

    // 获取分页对象的公共接口
    private Map<String,Object> descrbePageResult(IPage page, String title){
        Map<String,Object> result = Maps.newHashMap();

        result.put("totalSize",page.getTotal());
        result.put("totalPages",page.getPages());
        result.put("pageSize",page.getSize());
        result.put("nowPage",page.getCurrent());

        result.put(title, page.getRecords());

        return result;
    }
}
